package cn.blog.dao.front;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cn.blog.dao.conn.DataManager;
import cn.blog.dao.conn.ParamSet;
import cn.blog.dao.conn.RowDatas;
import cn.blog.entity.origin.Commet;

/**
 * 评论dao层
 * @author 江明
 * @time 2018年3月18日
 * @function
 */
public class ComDao extends DataManager{
	private RowDatas<Integer> comNum;
	private RowDatas<Commet> com;
	
	/**
	 * 无参构造	初始化
	 */
	public ComDao() {
		comNum = new comNumData();
		com = new comData();
	}
	
	/**
	 * 根据文章编号获取文章评论数
	 * @param artId 文章编号
	 * @return	评论数
	 */
	public int comNum(Integer artId) {
		String sql = "SELECT	COUNT(com_id)	FROM tb_comment	WHERE art_id = ?";
		return query(sql, new ParamSet(artId), comNum);
	}
	
	/**
	 * 获取评论表里的文章编号
	 * @return
	 */
	public List<Integer> comArtID(){
		String sql ="SELECT	art_id	FROM tb_comment";
				return queryList(sql, comNum);
	}
	
	/**
	 * 根据文章编号查询评论
	 * @param artId 文章编号
	 * @return	评论集合
	 */
	public List<Commet> comAll(int artId){
		String sql = "SELECT * FROM tb_comment WHERE art_id =?";
		return queryList(sql, new ParamSet(artId), com);
	}
	
	public int addCom(int articleId, int userId, String comPeoName, String comText, Date comTime) {
		String  sql = "INSERT INTO tb_comment (art_id,com_people_id,com_people_name,com_context,com_time) VALUES (?,?,?,?,?)";
		return updateExecuted(sql, new ParamSet(articleId, userId, comPeoName, comText, comTime));
	}
	public List<Commet> getComAll(){
		String sql = "SELECT * FROM tb_comment";
		return queryList(sql, com);
		
	}
	public int delComment(int id){
		String sql = "DELETE FROM tb_comment WHERE com_id=?";
		return updateExecuted(sql, new ParamSet(id));
	}
	
	private class comNumData implements RowDatas<Integer>{

		@Override
		public Integer getRowData(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			return rs.getInt(1);
		}
		
	}
	
	
	
	private class comData implements RowDatas<Commet>{
		
		@Override
		public Commet getRowData(ResultSet rs) throws SQLException {
			/*private Integer comId;//评论编号
			private Integer artId;//文章编号
			private Integer comPeopleId;//评论者编号
			private String comPeopleName;//评论者姓名
			private String comContext;//评论内容
			private Date comTime;//评论时间
*/			
			Commet com = new Commet();
			com.setArtId(rs.getInt(1));
			com.setArtId(rs.getInt(2));
			com.setComPeopleId(rs.getInt(3));
			com.setComPeopleName(rs.getString(4));
			com.setComContext(rs.getString(5));
			com.setComTime(rs.getDate(6));
			return com;
		}
		
	}

	
}
